package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.sunnyday.common.SunnydayException;
import com.xrit.sunnyday.dao.MemberMapper;
import com.xrit.sunnyday.dao.OrderMapper;
import com.xrit.sunnyday.dao.OrderSettingMapper;
import com.xrit.sunnyday.pojo.Member;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private  OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public Order addOderMemberOrderSetting(Map<String, String> map) throws SunnydayException{
            //1,获取日期
            String orderDate = map.get("orderDate");
            //2,要添加到数据库我们需要解析一下
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date=null;
            try {
                //变成数据库要的格式
                 date = dateFormat.parse(orderDate);
            } catch (ParseException e) {
                throw new SunnydayException("日期格式错误");
            }
            //3,调用根据日期查询的预约设置表方法
            OrderSetting orderSetting = orderSettingMapper.findOrderDate(date);
            //4,判断如果不存在
            if(orderSetting==null){
                //抛出自定义异常,提示用户,今天是休息日哦,不能预约
                throw new SunnydayException("今天是休息日哦,不能预约");
            }
            //5,存在则判断是否已经预约满了
            //如果已预约人数==可预约人数
            if(orderSetting.getReservations()>=orderSetting.getNumber()){
                //代表预约已满,不能预约
                throw new SunnydayException("这天预约人数已满");
            }
            //6,没有满 则判断是否是会员
            //获取手机号
            String telephone = map.get("telephone");
            //调用根据手机号查询会员表的方法
            Member member =memberMapper.findByTelephone(telephone);
            if(member==null){
                //为空,代表不是会员
                Member m = new Member();
                m.setIdCard(map.get("idCard"));
                m.setName(map.get("name"));
                m.setPhoneNumber(telephone);
                m.setRegTime(new Date());//给系统当前时间
                m.setSex(map.get("sex"));
                //调用添加到会员表的方法
                memberMapper.addMember(m);
            }
            //7,是会员:(判断该订单是否重复)
            //调用根据会员id,日期,套餐id查询订单的方法
            //查询条件
            Order order = new Order();
            order.setOrderDate(date);
            order.setMemberId(member.getId());
            order.setSetmealId(Integer.valueOf(map.get("setmealId")));
            Order o=orderMapper.findByMemberIdDateSetMealId(order);
            if(o!=null){
                //查询结果不为空 代表重复了同一时间同一用户表里面已有该订单
                //抛出异常
                throw new SunnydayException("不能重复预约");
            }
            //补充订单信息
            order.setOrderType(Order.ORDERSTATUS_NO);
            order.setOrderType(map.get("orderType"));
            //8,没有重复则调用添加到订单表的方法
            orderMapper.addOrder(order);
            //9,调用根据日期和可预约人数大于已预约人数修改的方法
            orderSettingMapper.updateByDate(orderSetting);

        return order;
    }

    @Override
    public Map<String, Object> findById(Integer id) {
        return orderMapper.findById(id);
    }
}
